﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Converter;
using csla4Skeleton.UI.SL.Resources.Utility;
using csla4Skeleton.UI.SL.DanhMuc.DM_HangHoa;
using csla4Skeleton.UI.SL.DanhMuc.DM_TinhChatHangHoa;
using csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.DM_LoaiDoiTuongTHCP;
using Accounting.Library;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System.ComponentModel;
using Utility;
using Accounting.Library.Utility;

namespace csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.DM_DoiTuongTHCP
{
    public partial class DM_DoiTuongTHCP_ChildWindow: ChildWindow
    {

        #region ISupportNavigation Members
        List<Control> controls = new List<Control>();
        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            try
            {
                if (parameters == "")
                    Khoa = null;
                else
                    Khoa = Int32.Parse(parameters);
            }
            catch
            {
                Khoa = null;
            }
        }

        public string Title
        {
            get { return "DoiTuongTHCP"; }
        }

       
        #endregion

        #region Load Methods
        private System.Nullable<int> Khoa = null;
        public string SelectedThanhPham = "";
        private int Index;
        public delegate void DoiTuongTHCPId_EventHandler(object sender, DoiTuongTHCPIDChangedEventArgs e);
        public event DoiTuongTHCPId_EventHandler  Completed;
        public DM_DoiTuongTHCP_ChildWindow(int Khoa, int Index)
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.DM_DoiTuongTHCP.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            
            InitializeComponent();
            this.Khoa = Khoa;
            this.Index = Index;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                
                System.Windows.Data.CollectionViewSource DM_DoiTuongTHCP_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_DoiTuongTHCP_ViewModelSource"];            
                DM_DoiTuongTHCP_ViewModel DM_DoiTuongTHCP_ViewModel = new DM_DoiTuongTHCP_ViewModel((int)Khoa);                   
                DM_DoiTuongTHCP_CollectionViewSource.Source = new List<object> { DM_DoiTuongTHCP_ViewModel };

                System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
                DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = new DM_HangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_HangHoa_CollectionViewSource.Source = new List<object> { dm_HangHoa_ListViewModel };

                this.DM_DoiTuongTHCP_ThanhPhamGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.DM_DoiTuongTHCP_ThanhPhamGridView);
            }
        }

        #endregion

        #region Event Handle
      
      
        private RadGridView GetActiveGridView()
        {
            return DM_DoiTuongTHCP_ThanhPhamGridView;
        }

        private void OKItemButton_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Data.CollectionViewSource DM_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
            DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel =  ((List<object>)DM_HangHoa_CollectionViewSource.Source)[0] as DM_HangHoa_ListViewModel;
            IList<DM_HangHoaInfo> listHangHoa = dm_HangHoa_ListViewModel.Model.getListOfInfo();
            foreach (DM_DoiTuongTHCP_ThanhPhamChild item in this.DM_DoiTuongTHCP_ThanhPhamGridView.SelectedItems)
            {
                foreach(DM_HangHoaInfo itemHangHoa in listHangHoa)
                {
                    if(item.HangHoaId == itemHangHoa.Khoa)
                    {
                        if (SelectedThanhPham.Equals(""))
                        {
                            SelectedThanhPham = itemHangHoa.Ma;
                        }
                        else
                        {
                            SelectedThanhPham = SelectedThanhPham + ";" + itemHangHoa.Ma;
                        }
                        break;
                    }
                }
                
            }
            if(Completed != null)
            {
                Completed(this, new DoiTuongTHCPIDChangedEventArgs(this.Index));
            }

        }

        private void CancelItemButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
      
        #endregion

        #region View Model Related Methods
        private DM_DoiTuongTHCP_ViewModel GetViewModel()
        {
            System.Windows.Data.CollectionViewSource DM_DoiTuongTHCP_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_DoiTuongTHCP_ViewModelSource"];
            return ((List<object>)DM_DoiTuongTHCP_CollectionViewSource.Source)[0] as DM_DoiTuongTHCP_ViewModel;
        }

        #endregion

       

       
        
    }
}
